package com.wing.purchase.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.purchase.model.entity.PurchaseStorageWork;
import com.wing.purchase.model.response.PurchaseStorageWorkVO;
import com.wing.purchase.service.PurchaseStorageWorkService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.purchase.model.request.PurchaseStorageWorkForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-采购入库作业管理", tags = "采购入库作业管理")
@RestController("appPurchaseStorageWorkController")
@RequestMapping("purchase/app/purchaseStorageWork")
public class PurchaseStorageWorkController {

    @Autowired
    private PurchaseStorageWorkService purchaseStorageWorkService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<PurchaseStorageWorkVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page,
            @RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit,
            @RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort,
            @RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order,
            String workNo,
            String skuName,
            String skuCode,
            String warehouseShelfSpaceCode,
            String startCreateTime,
            String endCreateTime
    ) {
        Page<PurchaseStorageWorkVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("workNo", workNo);
        params.put("skuName", skuName);
        params.put("skuCode", skuCode);
        params.put("warehouseShelfSpaceCode", warehouseShelfSpaceCode);
        params.put("startCreateTime", startCreateTime);
        params.put("endCreateTime", endCreateTime);

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<PurchaseStorageWorkVO> list = purchaseStorageWorkService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long purchaseStorageWorkId) {
        PurchaseStorageWorkVO purchaseStorageWorkVO = purchaseStorageWorkService.get4appById(purchaseStorageWorkId);
        if (purchaseStorageWorkVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), purchaseStorageWorkVO);
    }

    @PostMapping()
    public JsonResult add(@Validated PurchaseStorageWorkForm purchaseStorageWorkForm) {
        if (purchaseStorageWorkService.add(purchaseStorageWorkForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
